﻿Imports Fgv.Framework.UI
Imports LS.Leilao.Dominio

Partial Class UserControls_UsuarioCadastro
    Inherits BaseUserControl

#Region "Atributos da pagina"

    Public Enum Visoes
        NovoUsuario
        AtualizacaoSenha
    End Enum

    Public Property PessoaSelecionada() As Pessoa
        Get
            Return TryCast(ViewState("_pessoaSelecionada"), Pessoa)
        End Get
        Set(ByVal value As Pessoa)
            ViewState("_pessoaSelecionada") = value
            Me.DataBindUsuario()
        End Set
    End Property

#End Region

#Region "Delegates"

    Protected Delegate Sub PageDelegate()

#End Region

#Region "Classes Injetadas"


#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

        End If

    End Sub

#Region "Funcoes de Databind"

    Protected Sub ResetFormulario()

        txLogin.Text = String.Empty
        txOldPass.Text = String.Empty
        txPass.Text = String.Empty
        txConfirmPass.Text = String.Empty
        ckHbl.Checked = False
        btAtuUsu.Visible = False
        btSlvNovaSen.Visible = False
        btSlvUsu.Visible = False

    End Sub

    Protected Sub DataBindUsuario()

        Me.ResetFormulario()

        If Me.PessoaSelecionada Is Nothing Then
            txLogin.Visible = False
            txOldPass.Visible = False
            txConfirmPass.Visible = False
            txPass.Visible = False
            btAtuUsu.Visible = False
            btSlvNovaSen.Visible = False
            btSlvUsu.Visible = False
        End If

        If Me.PessoaSelecionada IsNot Nothing Then

            If Me.PessoaSelecionada.Usuario Is Nothing Then
                txLogin.Visible = True
                txOldPass.Visible = False
                txPass.Visible = True
                txConfirmPass.Visible = True
                btAtuUsu.Visible = False
                btSlvNovaSen.Visible = False
                btSlvUsu.Visible = True
            End If
        End If

            If Me.PessoaSelecionada.Usuario IsNot Nothing Then
                txLogin.Visible = True
                txOldPass.Visible = True
                txPass.Visible = True
                txConfirmPass.Visible = True
                btAtuUsu.Visible = False
                btSlvNovaSen.Visible = True
                btSlvUsu.Visible = False

                txLogin.Text = Me.PessoaSelecionada.Usuario.Login
                If Me.PessoaSelecionada.Usuario.Habilitado Then
                    ckHbl.Checked = True
                Else
                    ckHbl.Checked = False
                End If

            End If

    End Sub

#End Region

#Region "Operacoes do Usuario"

    Protected Sub btSlvNovaSen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btSlvNovaSen.Click
        If Me.SenhasAntigaENovaValidas() Then
            Throw New NotImplementedException
        End If
    End Sub

    Protected Sub btSlvUsu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btSlvUsu.Click

        Throw New NotImplementedException
    End Sub

#End Region

    Protected Function CadastroValido() As Boolean

        Dim _pass As String
        Dim _login As String
        Dim _passConfirm As String

        _login = txLogin.Text.Trim
        _pass = txPass.Text.Trim
        _passConfirm = txConfirmPass.Text.Trim

        If String.IsNullOrEmpty(_pass) OrElse String.IsNullOrEmpty(_passConfirm) Then
            Return False
        End If

        If _pass = _passConfirm AndAlso Not Me.LoginJahExiste(_login) Then
            Return True
        End If

        Return False

    End Function

    Protected Function LoginJahExiste(ByVal _login As String) As Boolean
        Return False
    End Function

    Protected Function SenhasAntigaENovaValidas() As Boolean

        Dim _pass As String
        Dim _passAntigo As String
        Dim _passConfirm As String

        _pass = txPass.Text.Trim
        _passAntigo = txOldPass.Text.Trim
        _passConfirm = txConfirmPass.Text.Trim

        If String.IsNullOrEmpty(_pass) OrElse String.IsNullOrEmpty(_passAntigo) OrElse String.IsNullOrEmpty(_passConfirm) Then
            Return False
        End If

        If _pass = _passConfirm And Me.Autentica(Me.PessoaSelecionada.Usuario, _passAntigo) Then
            Return True
        End If

        Return False

    End Function

    Protected Function Autentica(ByVal _usuario As Usuario, ByVal _pass As String) As Boolean
        'Me.PessoaSelecionada.Usuario 
        Return True
    End Function

#Region "Classes Auxiliares"

#End Region

End Class
